package com.tanhua.common.utils;

import java.util.regex.Pattern;

public class ChineseNumToIntUtil {
    public static int parse(String chineseNum) {
        int result = 0;
        char c = 0;

        boolean flag = Pattern.matches("^.*亿.*万.*$", chineseNum);

        for (int i = 0; i < chineseNum.length(); i++) {
            switch (chineseNum.charAt(i)) {
                case '零':
                    break;
                case '一':
                    c = 1;
                    break;
                case '二':
                    c = 2;
                    break;
                case '三':
                    c = 3;
                    break;
                case '四':
                    c = 4;
                    break;
                case '五':
                    c = 5;
                    break;
                case '六':
                    c = 6;
                    break;
                case '七':
                    c = 7;
                    break;
                case '八':
                    c = 8;
                    break;
                case '九':
                    c = 9;
                    break;
                case '十':
                    result += (c == 0 ? 10 : c * 10);
                    c = 0;
                    break;
                case '百':
                    result += c * 100;
                    c = 0;
                    break;
                case '千':
                    result += c * 1000;
                    c = 0;
                    break;
                case '万':
                    result = (result + c) * 10000;
                    c = 0;
                    break;
                case '亿':
                    if (flag) {
                        result = (result + c) * 10000;
                    } else {
                        result = (result + c) * 100000000;
                    }
                    c = 0;
                    break;
                default:
                    c = 0;
            }

        }
        if (c != 0)
            result += c;
        return result;
    }
}
